package cn.dglydrpy.fundmanagement.controller;

import cn.dglydrpy.fundmanagement.service.ITransactionService;
import cn.dglydrpy.fundmanagement.service.dto.TransactionDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/transactions")
public class TransactionController {

    @Autowired
    private ITransactionService transactionService;

    @PostMapping("/addTransaction")
    public void addTransaction(@RequestBody TransactionDto transactionDto) {
        transactionService.saveTransaction(transactionDto);
    }

    @PostMapping("/updateTransaction")
    public void updateTransaction(@RequestBody TransactionDto transactionDto) {
        transactionService.updateTransaction(transactionDto);
    }

    @PostMapping("/deleteTransaction/{id}")
    public void deleteTransaction(@PathVariable Long id) {
        transactionService.deleteTransaction(id);
    }

    @GetMapping("/{id}")
    public TransactionDto getTransactionById(@PathVariable Long id) {
        return transactionService.getTransactionById(id);
    }

    @GetMapping("/getTransactionsByConditions")
    public Page<TransactionDto> getTransactionsByConditions(
            @RequestParam String fundCode,
            @RequestParam String transactionType,
            @RequestParam int pageNo,
            @RequestParam int pageSize) {
        return transactionService.getTransactionsByConditions(fundCode, transactionType, pageNo, pageSize);
    }

    @GetMapping("/getAllTransactions")
    public List<TransactionDto> getAllTransactions() {
        return transactionService.getAllTransactions();
    }

    @GetMapping("/getTransactionsByFundCode")
    public List<TransactionDto> getTransactionsByFundCode(@RequestParam String fundCode) {
        return transactionService.getTransactionsByFundCode(fundCode);
    }

    @GetMapping("/getTotalProfit")
    public String getTotalProfit() {
        return transactionService.getTotalProfit().toString();
    }
}